/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年5月30日
 */
package com.massyframework.beanskin.util.exceptioncollect;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 例外收集组
 * <p>
 * 在运行框架初始化、启停等过程中，收集各模块抛出的例外
 * @author huangkh
 *
 */
public final class ExceptionGroup {
	
	private List<Exception> items;

	/**
	 * 构造方法
	 */
	public ExceptionGroup() {
		this.items = new CopyOnWriteArrayList<>();
	}
	
	/**
	 * 添加例外
	 * @param exception
	 */
	public void addException(Exception exception) {
		if (exception != null) {
			this.items.add(exception);
		}
	}

	/**
	 * 移除所有例外
	 * @return {@link List}
	 */
	public List<Exception> removeAll(){
		List<Exception> result = new ArrayList<>(this.items);
		this.items.clear();
		return result;
	}
	
	/**
	 * 是否为空组。
	 * @return {@link boolean},返回{@code true}表示没有例外，否则返回{@code false}
	 */
	public boolean isEmpty() {
		return this.items.isEmpty();
	}

	@Override
	public String toString() {
		return "ModuleExceptionGroup";
	}
}
